package com.liang.common.components;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.HashSet;
import java.util.Set;

/**
 * @name: OnlineListen
 * @author: 李昂
 * @date: 2020-12-11 13:40
 * @description：OnlineListen 监听用户的在线状态  缓存管理在线人数
 * @update: 2020-12-11 13:40
 */
public class OnlineListener {

    private static Cache<String, Set> cache = CacheBuilder.newBuilder().build();

    /**
     * @Author 李昂
     * @Description 保存登录的用户
     * @Date 2020/12/11 20:03
     */
    public static Set setOnline(String username) {

        Set set = null;
        System.out.println(getOnline());
        if (getOnline() != null) {
            set = getOnline();
        } else {
            set = new HashSet();
        }
        set.add(username);
        cache.put("count", set);
        return set;
    }


    public static Set getOnline() {
        return cache.getIfPresent("count");
    }


}








